/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package q2;

import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author c0909h1200
 */
public class ThreadOne extends Thread {
    Thread t;
    Data d;
    public ThreadOne(Data d){
        this.d= d;

        t= new Thread(this);
        t.start();

    }
    @Override
    public void run(){
        //run forever
        while (true) {

            //synchronise
            synchronized(d) {

                //check flag
                //if false get random
                if (!d.flag) {
                    //--get random
                    d.getRandom();

                    //--change flag
                    d.flag = true;

                    //--notify
                    d.notify();
                }

                //if true wait
                try {
                    d.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadOne.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

}
